#define DEBUG
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <utility>

using namespace std;

class Treap {
public:
  typedef pair<Treap*, Treap *> Pair;

  Treap *left, *right;
  int key, properity, size, cnt;

  Treap(int key) {
    this->key = key;
    cnt = size = 1;
    left = right = NULL;
    properity = rand();
  }

  int getSize() {
    return this ? size : 0;
  }

  Pair split(int k) {
    if (!this) {
      return Pair(NULL, NULL);
    }
    if (key<=k) {
      Pair t = right->split(k);
      right = t.first;
      size = cnt+left->getSize()+right->getSize();
      return Pair(this, t.second);
    } else {
      Pair t = left->split(k);
      left = t.second;
      size = cnt+left->getSize()+right->getSize();
      return Pair(t.first, this);
    }
  }

  static Treap* merge(Treap *l, Treap *r) {
    if (!l) {
      return r;
    }
    if (!r) {
      return l;
    }
    if (l->properity<=r->properity) {
      r->left = merge(l, r->left);
      r->size = r->cnt + r->left->getSize() + r->right->getSize();
      return r;
    } else {
      l->right = merge(l->right, r);
      l->size = l->cnt + l->left->getSize() + l->right->getSize();
      return l;
    }
  }

  int findByRank(int rank) {
    if (rank<=left->getSize()) {
      return left->findByRank(rank);
    } else if (rank<=left->getSize()+cnt) {
      return key;
    } else {
      return right->findByRank(rank-left->getSize()-cnt);
    }
  }

  int getFirst() {
    return left ? left->getFirst() : key;
  }

  int getLast() {
    return right ? right->getLast() : key;
  }
};

int main() {
#ifdef DEBUG
  freopen("src.in", "r", stdin);
  freopen("src.out", "w", stdout);
#endif

  srand(time(0));

  Treap *root=NULL;
  int n;
  scanf("%d", &n);
  for (int i=1; i<=n; i++) {
    int opt, x;
    Treap::Pair p1, p2;
    scanf("%d %d", &opt, &x);
    switch (opt) {
    case 1:
      p1=root->split(x-1), p2=p1.second->split(x);
      if (p2.first) {
	p2.first->cnt++;
	p2.first->size++;
      } else {
	p2.first = new Treap(x);
      }
      root = Treap::merge(p1.first, Treap::merge(p2.first, p2.second));
      break;

    case 2:
      p1=root->split(x-1), p2=p1.second->split(x);
      p2.first->cnt--, p2.first->size--;
      if (!p2.first->cnt) {
	root = Treap::merge(p1.first, p2.second);
      } else {
	root = Treap::merge(p1.first, Treap::merge(p2.first, p2.second));
      }
      break;

    case 3:
      p1=root->split(x-1);
      printf("%d\n", p1.first->size+1);
      root = Treap::merge(p1.first, p1.second);
      break;

    case 4:
      printf("%d\n", root->findByRank(x));
      break;

    case 5:
      p1 = root->split(x-1);
      printf("%d\n", p1.first->getLast());
      root = Treap::merge(p1.first, p1.second);
      break;

    case 6:
      p1 = root->split(x+1);
      printf("%d\n", p1.second->getFirst());
      root = Treap::merge(p1.first, p1.second);
      break;
    }
  }

  fclose(stdin);
  fclose(stdout);
  return 0;
}
